﻿#include "detailadd.h"
#include "ui_detailadd.h"

DetailAdd::DetailAdd(QWidget *parent,const QString &uid) :
    QDialog(parent),
    ui(new Ui::DetailAdd)
{
    ui->setupUi(this);

    _uid=uid;
    indexTable=TD_EPIBOLY_INDEX;
    detailTable=TD_EPIBOLY_DETAIL;

    int index_id=getId(indexTable,_uid);
    ui->name->setText(getValue(indexTable,"name",index_id));
}

DetailAdd::~DetailAdd()
{
    delete ui;
}

int DetailAdd::getId(const QString &table, const QString &uid) const
{
    QSqlQuery query;
    query.exec(QString("select id from %1 where uid='%2'")
               .arg(table)
               .arg(uid));
    query.next();
    return query.value(0).toInt();
}

QString DetailAdd::getValue(const QString &table, const QString &key, int id) const
{
    QSqlQuery query;
    query.exec(QString("select %1 from %2 where id='%3'")
               .arg(key)
               .arg(table)
               .arg(id));
    query.next();
    return query.value(0).toString();
}

bool DetailAdd::saveRecord()
{
    QSqlQuery query;
    query.prepare("insert into td_epiboly_detail (uid,date,name,number,status,remark) values (?,?,?,?,?,?)");
    query.addBindValue(_uid);
    query.addBindValue(Env::today);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(ST_WAIT);
    query.addBindValue(remark);
    return query.exec();
}

void DetailAdd::on_submit_clicked()
{
    name=ui->name->text().trimmed();
    number=ui->number->text().trimmed();
    if(name.isEmpty() || number.isEmpty())
    {
        QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"));
        return;
    }
    remark=ui->remark->text().trimmed();
    if(saveRecord())
        accept();
    else
        QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("提交失败！数据库异常"));
}
